﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using EM.Lib;


namespace DSM.Administration.UserGroupPageRel
{
    public partial class ugpr : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            Authentication ua = new Authentication();
            ua.CheckRolePageAuthentication(4, Authentication.RedirectionURLTypes.LoginPage);


            if (!IsPostBack)
            {
                FillCombo fc = new FillCombo();
                fc.User(ddlUserGroup, true);
            }
        }

        #region GetData (LeftPageGetData, RightPageGetData)
        private void LeftPageGetData()
        {

            SqlCommand cmd = new SqlCommand();
            DataSrv srv = new DataSrv();
            DataTable dt = new DataTable();

            string sSQL = "";
            sSQL += " SELECT PAGEID, PAGENAME";
            sSQL += " FROM TBLPAGES P";
            sSQL += " WHERE  PAGEID NOT IN (SELECT  PAGEID FROM TBLUSERPAGE WHERE USERID=@USERID)";
            sSQL += " AND PAGEPARENT <> -1 ";
            sSQL += " ORDER BY PAGEORDER ";

            cmd.CommandText = sSQL;

            cmd.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;

            dt = srv.CreateDataTable(cmd, "TBLLOOKUPVALUE");

            lbLeftPage.DataSource = dt;
            lbLeftPage.DataValueField = dt.Columns[0].ToString();
            lbLeftPage.DataTextField = dt.Columns[1].ToString();
            lbLeftPage.DataBind();
        }

        private void RightPageGetData()
        {

            SqlCommand cmd = new SqlCommand();
            DataSrv srv = new DataSrv();
            DataTable dt = new DataTable();

            string sSQL = "";
            sSQL += " SELECT P.PAGEID, P.PAGENAME ";
            sSQL += " FROM TBLPAGES P , TBLUSERPAGE  UG";
            sSQL += " WHERE P.PAGEID = UG.PAGEID";
            sSQL += " AND UG.USERID = @USERID ";
            sSQL += " AND PAGEPARENT <> -1 ";
            sSQL += " ORDER BY P.PAGEORDER";


            cmd.CommandText = sSQL;
            cmd.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;


            dt = srv.CreateDataTable(cmd, "TBLLOOKUPVALUE");

            lbRightPage.DataSource = dt;
            lbRightPage.DataValueField = dt.Columns[0].ToString();
            lbRightPage.DataTextField = dt.Columns[1].ToString();
            lbRightPage.DataBind();

        }
        #endregion


        protected void btnLefttoright_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            DataSrv srv = new DataSrv();

            string sSQL = "";
            sSQL += " INSERT INTO TBLUSERPAGE  (PAGEID,  USERID)";
            sSQL += " SELECT PAGEID,  @USERID  FROM  TBLPAGES";
            sSQL += " WHERE PAGEID IN (" + sLbArray(lbLeftPage, typeof(int)) + ") ";

            sSQL += " INSERT INTO TBLUSERPAGE  (PAGEID,  USERID)";
            sSQL += " SELECT PAGEPARENT,  @USERID  FROM  TBLPAGES";
            sSQL += " WHERE PAGEID IN (" + sLbArray(lbLeftPage, typeof(int)) + ") ";
            sSQL += " AND PAGEPARENT NOT IN (SELECT PAGEID FROM TBLUSERPAGE  WHERE USERID  =  @USERID) ";
            sSQL += " AND PAGEPARENT <> 0 ";




            cmd.CommandText = sSQL;
            cmd.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;

            srv.FnExecuteNonQuery(cmd);
            LeftPageGetData();
            RightPageGetData();
        }
        protected void btnRightAll_Click(object sender, EventArgs e)
        {

            DataSrv srv = new DataSrv();
            SqlCommand CMD = new SqlCommand();
            DataTable dt = new DataTable();

            string ssql = "";
            ssql += "  SELECT PAGEID FROM TBLPAGES ";
            ssql += "  WHERE  PAGEID NOT IN (SELECT  PAGEID FROM TBLUSERPAGE WHERE USERID=@USERID)";


            CMD.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;
            CMD.CommandText = ssql;
            dt = srv.CreateDataTable(CMD, "TBLPAGES");


            for (int i = 0; i < dt.Rows.Count; i++)
            {
                SqlCommand cmd = new SqlCommand();
                string sSQL = "";
                sSQL += " INSERT INTO TBLUSERPAGE (PAGEID,   USERID) ";
                sSQL += " SELECT @PAGEID,  @USERID";

                cmd.CommandText = sSQL;
                cmd.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;
                cmd.Parameters.Add("@PAGEID", SqlDbType.NVarChar).Value = dt.Rows[i]["PAGEID"].ToString();


               
              srv.FnExecuteNonQuery(cmd);
            }
            LeftPageGetData();
            RightPageGetData();

        }

        protected void btnRigthToLeft_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            DataSrv srv = new DataSrv();

            string sSQL = "";
            sSQL += " DELETE FROM  TBLUSERPAGE ";
            sSQL += " WHERE PAGEID IN (" + sLbArray(lbRightPage, typeof(string)) + ") ";
            sSQL += " AND USERID = @USERID";


            sSQL += " DELETE FROM  TBLUSERPAGE ";
            sSQL += " WHERE PAGEID IN (SELECT  PAGEID FROM TBLPAGES WHERE PAGEPARENT  IN (" + sLbArray(lbRightPage, typeof(string)) + ") )";
            sSQL += " AND USERID = @USERID";


            cmd.CommandText = sSQL;
            cmd.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;

           
             srv.FnExecuteNonQuery(cmd);

            LeftPageGetData();
            RightPageGetData();
        }
        protected void BtnLeftAll_Click(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand();
            DataSrv srv = new DataSrv();


            string sSQL = "";
            sSQL += " DELETE FROM TBLUSERPAGE ";
            sSQL += " WHERE USERID = @USERID ";

            cmd.Parameters.Add("USERID", SqlDbType.Int).Value = ddlUserGroup.SelectedValue;
            cmd.CommandText = sSQL;
            
             srv.FnExecuteNonQuery(cmd);
          
            LeftPageGetData();
            RightPageGetData();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (ddlUserGroup.SelectedValue !="0")
            {

              
                LeftPageGetData();
                RightPageGetData();    
            }
            else
            {
                Response.Redirect(Request.RawUrl);
            }
        }

      

        private string sLbArray(ListBox lb, Type type)
        {
            string IDs = "";
            if (type == typeof(string))
            {
                foreach (int item in lb.GetSelectedIndices())
                {
                    IDs += "'" + lb.Items[item].Value + "',";
                }
            }
            else if (type == typeof(int))
            {
                foreach (int item in lb.GetSelectedIndices())
                {
                    IDs += lb.Items[item].Value + ",";
                }
            }
            return IDs.TrimEnd(',');
        }
    }
}
